//
//  WPUserInfoViewModel.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/8.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPUserInfoViewModel.h"
#import "URL.h"

@implementation WPUserInfoViewModel
+ (void)refreshUserDataWithSuccessBlock:(void (^)(NSArray *))success andWithFailureBlcok:(void (^)(NSString *))failure {
    [NetWorkingManager postWithUrl:@"moveSoftware/updateMoney.jhtml" requestParams:[NetWorkingManager paramsByAppendingUserInfo:@{@"memberid":[UserModel defaultUser].userID}] SuccessHandler:^(id responseObject) {
        if (responseObject[@"resultType"]&&[responseObject[@"resultType"] intValue]) {
            NSString * balance = [NSString stringWithFormat:@"%@", responseObject[@"resultMsg"]?responseObject[@"resultMsg"]:@"0"];
            NSString * orderCount = [NSString stringWithFormat:@"%@", responseObject[@"orderCount"] ? responseObject[@"orderCount"]:@"0"];
            [UserDataStoreModel saveUserDataWithDataKey:@"balance" andWithData:balance andWithReturnFlag:nil];
            success(@[balance, orderCount]);
        } else {
            failure(@"错误");
        }
    } failureHandler:^(NSError *error) {
        failure(error.localizedDescription);
    }];
}
+ (void)refreshMsgCountWithSuccessBlock:(void (^)(NSString *))success andWithFailureBlock:(void (^)(NSString *))failure {
    [NetWorkingManager postWithUrl:@"moveSoftware/messageNumber.jhtml" requestParams:[NetWorkingManager paramsByAppendingUserInfo:@{@"memberid":[UserModel defaultUser].userID}] SuccessHandler:^(id responseObject) {
        if (responseObject[@"resultType"]&&[responseObject[@"resultType"] intValue]) {
            success([NSString stringWithFormat:@"%@", responseObject[@"resultMsg"]]);
        } else {
            success(@"0");
        }
    } failureHandler:^(NSError *error) {
        failure(error.localizedDescription);
    }];
}
+ (void)updateHeadImageWithImageData:(NSData *)imageData andSuccessBlock:(void (^)(NSString *))success andFailureBlock:(void (^)(NSString *))failure {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UserModel defaultUser].userID, @"id", nil];
    [manager POST:[NSString stringWithFormat:@"%@/app/customer/updateInfor", BASIC_URL] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"headPicture" fileName:@"headImage.png" mimeType:@"png/jpg"];
    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (responseObject) {
            if ([responseObject[@"success"] boolValue]) {
                success(@"修改成功");
            } else {
                failure(responseObject[@"message"]);
            }
        } else {
            failure(@"网络数据错误");
        }
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];

}
@end
